/*
	const.h
	Constants used by Ethernet Powerlink protocol v1.1.0
*/


#ifndef POWERLINK_HEADER_CONST
#define POWERLINK_HEADER_CONST

/*Powerlink message type id*/
#define MSGTYPE_SoC 0x01
#define MSGTYPE_PReq 0x03
#define MSGTYPE_PRes 0x04
#define MSGTYPE_SoA 0x05
#define MSGTYPE_ASnd 0x06
#define MSGTYPE_AMNI 0x07
#define MSGTYPE_AInv 0x0d

/*ASnd request priorities*/
#define PRIO_NMT_REQUEST 0b111
#define PRIO_H0_REQUEST 0b110
#define PRIO_H1_REQUEST 0b101
#define PRIO_H2_REQUEST 0b100
#define PRIO_GENERIC_REQUEST 0b011
#define PRIO_L0_REQEUST 0b010
#define PRIO_L1_REQUEST 0b001
#define PRIO_L2_REQUEST 0b000

/*ASnd Service id*/
#define IDENT_RESPONSE 0x01
#define STATUS_RESPONSE 0x02
#define NMT_REQUEST 0x03
#define NMT_COMMAND 0x04
#define SDO_SVCID 0x05

/*SoA requested service id*/
#define NO_SERVICE 0x00
#define IDENT_REQUEST 0x01
#define STATUS_REQUEST 0x02
#define NMT_REQUEST_INVITE 0x03
#define UNSPECIFIED_INVITE 0xff

/*NMT states*/
#define NMT_GS_OFF 0b00000000
#define NMT_GS_INITIALISING 0b00011001
#define NMT_GS_RESET_APPLICATION 0b00101001
#define NMT_GS_RESET_COMMUNICATION 0b00111001
#define NMT_GS_RESET_CONFIGURATION 0b01111001
#define NMT_CS_NOT_ACTIVE 0b00011100
#define NMT_CS_PRE_OPERATIONAL_1 0b00011101
#define NMT_CS_PRE_OPERATIONAL_2 0b01011101
#define NMT_CS_READY_TO_OPERATE 0b01101101
#define NMT_CS_OPERATIONAL 0b11111101
#define NMT_CS_STOPPED 0b01001101
#define NMT_CS_BASIC_ETHERNET 0b00011110
#define NMT_MS_NOT_ACTIVE 0b00011100
#define NMT_MS_PRE_OPERATIONAL_1 0b00011101
#define NMT_MS_PRE_OPERATIONAL_2 0b01011101
#define NMT_MS_READY_TO_OPERATE 0b01101101
#define NMT_MS_OPERATIONAL 0b11111101
#define NMT_MS_BASIC_ETHERNET 0b00011110
#define IS_NMT_GS_POWERED(code) ((code & 0b00001000) == 0b00001000)
#define IS_NMT_GS_INITIALISATION(code) ((code & 0b00001111) == 0b00001001)
#define IS_NMT_GS_COMMUNICATING(code) ((code & 0b00001100) == 0b00001100)
#define IS_NMT_CS_EPL_MODE(code) ((code & 0b00001111) == 0b00001101)
#define IS_NMT_MS_EPL_MODE(code) ((code & 0b00001111) == 0b00001101)

/*NMT Commands*/
#define CMD_IdentResponse 0x01
#define CMD_StatusResponse 0x02
#define CMD_NMTStartNode 0x21
#define CMD_NMTStopNode 0x22
#define CMD_NMTEnterPreOperational2 0x23
#define CMD_NMTEnableReadyToOperate 0x24
#define CMD_NMTResetNode 0x28
#define CMD_NMTResetCommunication 0x29
#define CMD_NMTResetConfiguration 0x2a
#define CMD_NMTSwReset 0x2b
#define CMD_NMTGoToStandby 0x2c
#define CMD_NMTStartNodeEx 0x41
#define CMD_NMTStopNodeEx 0x42
#define CMD_NMTEnterPreOperational2Ex 0x43
#define CMD_NMTEnableReadyToOperateEx 0x44
#define CMD_NMTResetNodeEx 0x48
#define CMD_NMTResetCommunicationEx 0x49
#define CMD_NMTResetConfigurationEx 0x4a
#define CMD_NMTSwResetEx 0x4b
#define CMD_NMTNetHostNameSet 0x62
#define CMD_NMTFlushArpEntry 0x63
#define CMD_NMTPublishConfiguredNodes 0x80
#define CMD_NMTPublishActiveNodes 0x90
#define CMD_NMTPublishPreOperational1 0x91
#define CMD_NMTPublishPreOperational2 0x92
#define CMD_NMTPublishReadyToOperate 0x93
#define CMD_NMTPublishOperational 0x94
#define CMD_NMTPublishStopped 0x95
#define CMD_NMTPublishNodeStates 0x96
#define CMD_NMTPublishEmergencyNew 0xa0
#define CMD_NMTPublishTime 0xb0
#define CMD_NMTInvalidService 0xff

/*general purpose constants*/
#define C_ADR_BROADCAST 0xff
#define C_ADR_DIAG_DEF_NODE_ID 0xfd
#define C_ADR_DUMMY_NODE_ID 0xfc
#define C_ADR_SELF_ADR_NODE_ID 0xfb
#define C_ADR_INVALID 0x00
#define C_ADR_MN_DEF_NODE_ID 0xf0
#define C_ADR_RT1_DEF_NODE_ID 0xfe
#define C_DLL_ASND_PRIO_NMTRQST 7
#define C_DLL_ASND_PRIO_STD 0
#define C_DLL_ETHERTYPE_EPL 0x88ab
#define C_DLL_ISOCHR_MAX_PAYL 1490
#define C_DLL_MAX_ASYNC_MTU 1500
#define C_DLL_MAX_PAYL_OFFSET 1499
#define C_DLL_MAX_RS 7
#define C_DLL_MIN_ASYNC_MTU 300
#define C_DLL_MIN_PAYL_OFFSET 45
#define C_DLL_MULTICAST_AMNI {0x01, 0x11, 0x1e, 0x00, 0x00, 0x05}
#define C_DLL_MULTICASE_ASND {0x01, 0x11, 0x1e, 0x00, 0x00, 0x04}
#define C_DLL_MULTICAST_PRES {0x01, 0x11, 0x1e, 0x00, 0x00, 0x02}
#define C_DLL_MULTICAST_SOA {0x01, 0x11, 0x1e, 0x00, 0x00, 0x03}
#define C_DLL_MULTICAST_SOC {0x01, 0x11, 0x1e, 0x00, 0x00, 0x01}
#define C_DLL_PREOP1_START_CYCLES 10
#define C_DLL_T_BITTIME 10
#define C_DLL_T_EPL_PDO_HEADER 10
#define C_DLL_T_ETH2_WRAPPER 18
#define C_DLL_T_IFG 960
#define C_DLL_T_MIN_FRAME 5120
#define C_DLL_T_PREAMBLE 640
#define C_ERR_MONITOR_DELAY 10
#define C_IP_ADR_INVALID 0x00000000
#define C_IP_INVALID_MTU 0
#define C_NMT_STATE_TOLERANCE 5
#define C_NMT_STATREQ_CYCLE 5
#define C_SDO_EPL_PORT 3819

/*Error code constants*/
#define E_DLL_BAD_PHYS_MODE 0x8161
#define E_DLL_COLLISION 0x8162
#define E_DLL_COLLISION_TH 0x8163
#define E_DLL_CRC_TH 0x8164
#define E_DLL_LOSS_OF_LINK 0x8165
#define E_DLL_MAC_BUFFER 0x8166
#define E_DLL_ADDRESS_CONFLICT 0x8201
#define E_DLL_MULTIPLE_MN 0x8202
#define E_PDO_SHORT_RX 0x8210
#define E_PDO_MAP_VERS 0x8211
#define E_NMT_ASND_MTU_DIF 0x8212
#define E_NMT_ASND_MTU_LIM 0x8213
#define E_NMT_ASND_TX_LIM 0x8214
#define E_NMT_CYCLE_LEN 0x8231
#define E_DLL_CYCLE_EXCEED 0x8232
#define E_DLL_CYCLE_EXCEED_TH 0x8233
#define E_NMT_IDLE_LIM 0x8234
#define E_DLL_JITTER_TH 0x8235
#define E_DLL_LATE_PRES_TH 0x8236
#define E_NMT_PREQ_CN 0x8237
#define E_NMT_PREQ_LIM 0x8238
#define E_NMT_PRES_CN 0x8239
#define E_NMT_PRES_RX_LIM 0x823a
#define E_NMT_PRES_TX_LIM 0x823b
#define E_DLL_INVALID_FORMAT 0x8241
#define E_DLL_LOSS_PREQ_TH 0x8242
#define E_DLL_LOSS_PRES_TH 0x8243
#define E_DLL_LOSS_SOA_TH 0x8244
#define E_DLL_LOSS_SOC_TH 0x8245
#define E_DLL_LOSS_STATUSRES_TH 0x8245
#define E_NMT_BA1 0x8410
#define E_NMT_BA1_NO_MN_SUPPORT 0x8411
#define E_NMT_BPO1 0x8420
#define E_NMT_BPO1_GET_IDENT 0x8421
#define E_NMT_BPO1_DEVICE_TYPE 0x8422
#define E_NMT_BPO1_VENDOR_ID 0x8423
#define E_NMT_BPO1_PRODUCT_CODE 0x8424
#define E_NMT_BPO1_REVISION_NO 0x8425
#define E_NMT_BPO1_SERIAL_NO 0x8426
#define E_NMT_BPO1_CF_VERIFY 0x8428
#define E_NMT_BPO1_SW_INVALID 0x8429
#define E_NMT_BPO1_SW_STATE 0x842a
#define E_NMT_BPO1_SW_UPDATE 0x842b
#define E_NMT_BPO1_SW_REJECT 0x842c
#define E_NMT_BPO2 0x8430
#define E_NMT_BRO 0x8440
#define E_NMT_WRONG_STATE 0x8480
#define IS_E_HW_ERROR(code) ((code & 0xfff0) == 0x8160)
#define IS_E_PROTOCOL_ERROR(code) ((code & 0xff00) == 0x8200)
#define IS_E_FRAMESIZE_ERROR(code) ((code & 0xfff0) == 0x8210)
#define IS_E_TIMING_ERROR(code) ((code & 0xfff0) == 0x8230)
#define IS_E_BOOTUP_ERROR(code) ((code & 0xff00) == 0x8400)

/*DLL CN cycle states*/
#define DLL_CS_NON_CYCLIC 0x00
#define DLL_CS_WAIT_SOC 0x01
#define DLL_CS_WAIT_PREQ 0x02
#define DLL_CS_WAIT_SOA 0x03

/*DLL CN cycle events*/
#define DLL_CE_SOC 0x00
#define DLL_CE_PREQ 0x01
#define DLL_CE_PRES 0x02
#define DLL_CE_SOA 0x03
#define DLL_CE_ASND 0x04
#define DLL_CE_SOC_TIMEOUT 0x05

/*DLL MN cycle states*/
#define DLL_MS_NON_CYCLIC 0x00
#define DLL_MS_WAIT_SOC_TRIG 0x01
#define DLL_MS_WAIT_PRES 0x02
#define DLL_MS_WAIT_ASND 0x03
#define DLL_MS_WAIT_SOA 0x04

/*DLL MN cycle events*/
#define DLL_ME_PRES 0x00
#define DLL_ME_PRES_TIMEOUT 0x01
#define DLL_ME_ASND 0x02
#define DLL_ME_ASND_TIMEOUT 0x03
#define DLL_ME_SOC_TRIG 0x04
#define DLL_ME_SOA_TRIG 0x05

#define TRUE 0b0
#define FALSE 0b1

#endif
